﻿// ******************************************************
// SMS.Framework
// SMS.Framework.Core
// Conversion.cs
// Copyright (c) 2009, SMS Framework. All Rights Reserved.
// ******************************************************

using System;

namespace SMS.Framework.Core
{

    /// <summary>
    /// Class used to convert metric units from the SI or International System of Units, temperatures and calendar dates.
    /// </summary>
	public class Conversion
	{

        /// <summary> 
        /// Function to convert square acres to square meters. 
        /// </summary> 
        /// <param name="value">Square acres to convert them into square meters.</param> 
        /// <returns>Returns the equivalent value in square meters.</returns> 
        /// <remarks></remarks> 
        public static double SquareAcreToSquareMeter(double value)
        {
            // 1 square acre = 4046.9 square meters 
            return value * 4046.9;
        }
        // SquareAcreToSquareMeter 

        /// <summary> 
        /// Function to convert square acres to square meters. 
        /// </summary> 
        /// <param name="value">Square acres to convert them into square meters.</param> 
        /// <returns>Returns the equivalent value in square meters.</returns> 
        /// <remarks></remarks> 
        public static double SquareAcreToSquareMeter(long value)
        {
            // 1 square acre = 4046.9 square meters 
            return value * 4046.9;
        }
        // SquareAcreToSquareMeter 

        /// <summary> 
        /// Function to convert square ha to square acres. 
        /// </summary> 
        /// <param name="value">Square ha to convert them into square acres.</param> 
        /// <returns>Returns the equivalent value in square acres.</returns> 
        /// <remarks></remarks> 
        public static double SquareHaToSquareAcre(double value)
        {
            // 1 square ha = 2.4711 square acre 
            return value * 2.4711;
        }
        // SquareHaToSquareAcre 

        /// <summary> 
        /// Function to convert square ha to square acres. 
        /// </summary> 
        /// <param name="value">Square ha to convert them into square acres.</param> 
        /// <returns>Returns the equivalent value in square acres.</returns> 
        /// <remarks></remarks> 
        public static double SquareHaToSquareAcre(long value)
        {
            // 1 square ha = 2.4711 square acre 
            return value * 2.4711;
        }
        // SquareHaToSquareAcre 

        /// <summary> 
        /// Function to convert square feet to square meters. 
        /// </summary> 
        /// <param name="value">Square feet to convert them into square meters.</param> 
        /// <returns>Returns the equivalent value in square meters.</returns> 
        /// <remarks></remarks> 
        public static double SquareFootToSquareMeter(double value)
        {
            // 1 square foot = 0.0929 square meters 
            return value * 0.0929;
        }
        // SquareFootToSquareMeter 

        /// <summary> 
        /// Function to convert square feet to square meters. 
        /// </summary> 
        /// <param name="value">Square feet to convert them into square meters.</param> 
        /// <returns>Returns the equivalent value in square meters.</returns> 
        /// <remarks></remarks> 
        public static double SquareFootToSquareMeter(long value)
        {
            // 1 square foot = 0.0929 square meters 
            return value * 0.0929;
        }
        // SquareFootToSquareMeter 

        /// <summary> 
        /// Function to convert square inches to square centimeters. 
        /// </summary> 
        /// <param name="value">Square inches to convert them into square centimeters.</param> 
        /// <returns>Returns the equivalent value in square centimeters.</returns> 
        /// <remarks></remarks> 
        public static double SquareInchToSquareCentimeter(double value)
        {
            // 1 square inch = 6.4516 square centimeters 
            return value * 6.4516;
        }
        // SquareInchToSquareCentimeter 

        /// <summary> 
        /// Function to convert square inches to square centimeters. 
        /// </summary> 
        /// <param name="value">Square inches to convert them into square centimeters.</param> 
        /// <returns>Returns the equivalent value in square centimeters.</returns> 
        /// <remarks></remarks> 
        public static double SquareInchToSquareCentimeter(long value)
        {
            // 1 square inch = 6.4516 square centimeters 
            return value * 6.4516;
        }
        // SquareInchToSquareCentimeter 

        /// <summary> 
        /// Function to convert square centimeters to square inches. 
        /// </summary> 
        /// <param name="value">Square centimeters to convert them into square inches.</param> 
        /// <returns>Returns the equivalent value in square inches.</returns> 
        /// <remarks></remarks> 
        public static double SquareCentimeterToSquareInch(double value)
        {
            // 1 square centimeter = 0.155 square inches 
            return value * 0.155;
        }
        // SquareCentimeterToSquareInch 

        /// <summary> 
        /// Function to convert square centimeters to square inches. 
        /// </summary> 
        /// <param name="value">Square centimeters to convert them into square inches.</param> 
        /// <returns>Returns the equivalent value in square inches.</returns> 
        /// <remarks></remarks> 
        public static double SquareCentimeterToSquareInch(long value)
        {
            // 1 square centimeter = 0.155 square inches 
            return value * 0.155;
        }
        // SquareCentimeterToSquareInch 

        /// <summary> 
        /// Function to convert square kilometers to square miles. 
        /// </summary> 
        /// <param name="value">Square kilometers to convert them into square miles.</param> 
        /// <returns>Returns the equivalent value in square miles.</returns> 
        /// <remarks></remarks> 
        public static double SquareKilometerToSquareMile(double value)
        {
            // 1 square kilometer = 0.3861 square mile 
            return value * 0.3861;
        }
        // SquareKilometerToSquareMile 

        /// <summary> 
        /// Function to convert square kilometers to square miles. 
        /// </summary> 
        /// <param name="value">Square kilometers to convert them into square miles.</param> 
        /// <returns>Returns the equivalent value in square miles.</returns> 
        /// <remarks></remarks> 
        public static double SquareKilometerToSquareMile(long value)
        {
            // 1 square kilometer = 0.3861 square mile 
            return value * 0.3861;
        }
        // SquareKilometerToSquareMile 

        /// <summary> 
        /// Function to convert square miles to square kilometers. 
        /// </summary> 
        /// <param name="value">Square miles to convert them into square kilometers.</param> 
        /// <returns>Returns the equivalent value in square kilometers.</returns> 
        /// <remarks></remarks> 
        public static double SquareMileToSquareKilometer(double value)
        {
            // 1 square mile = 2.59 square kilometer 
            return value * 2.59;
        }
        // SquareMileToSquareKilometer 

        /// <summary> 
        /// Function to convert square miles to square kilometers. 
        /// </summary> 
        /// <param name="value">Square miles to convert them into square kilometers.</param> 
        /// <returns>Returns the equivalent value in square kilometers.</returns> 
        /// <remarks></remarks> 
        public static double SquareMileToSquareKilometer(long value)
        {
            // 1 square mile = 2.59 square kilometer 
            return value * 2.59;
        }
        // SquareMileToSquareKilometer 

        /// <summary> 
        /// Function to convert square meters to square yards. 
        /// </summary> 
        /// <param name="value">Square meters to convert them into square yards.</param> 
        /// <returns>Returns the equivalent value in square yards.</returns> 
        /// <remarks></remarks> 
        public static double SquareMeterToSquareYard(double value)
        {
            // 1 square meter = 1.196 square yard 
            return value * 1.196;
        }
        // SquareMeterToSquareYard 

        /// <summary> 
        /// Function to convert square meters to square yards. 
        /// </summary> 
        /// <param name="value">Square meters to convert them into square yards.</param> 
        /// <returns>Returns the equivalent value in square yards.</returns> 
        /// <remarks></remarks> 
        public static double SquareMeterToSquareYard(long value)
        {
            // 1 square meter = 1.196 square yard 
            return value * 1.196;
        }
        // SquareMeterToSquareYard 

        /// <summary> 
        /// Function to convert square yards to square meters. 
        /// </summary> 
        /// <param name="value">Square yards to convert them into square meters.</param> 
        /// <returns>Returns the equivalent value in square meters.</returns> 
        /// <remarks></remarks> 
        public static double SquareYardToSquareMeter(double value)
        {
            // 1 square yard = 0.8361 square meters 
            return value * 0.8361;
        }
        // SquareYardToSquareMeter 

        /// <summary> 
        /// Function to convert square yards to square meters. 
        /// </summary> 
        /// <param name="value">Square yards to convert them into square meters.</param> 
        /// <returns>Returns the equivalent value in square meters.</returns> 
        /// <remarks></remarks> 
        public static double SquareYardToSquareMeter(long value)
        {
            // 1 square yard = 0.8361 square meters 
            return value * 0.8361;
        }
        // SquareYardToSquareMeter 


        // ºC :: Celsius - ºF :: Fahrenheit - K :: Kelvin 
        // ºR :: Rankine - ºRe :: Reaumur 

        // Water Boils 
        public static readonly byte CelsiusWaterBoils = 100;
        public static readonly byte FahrenheitWaterBoils = 212;
        public static readonly float KelvinWaterBoils = 373.15f;
        public static readonly float RankineWaterBoils = 671.67f;
        public static readonly byte ReaumurWaterBoils = 80;

        // Water Freezes 
        public static readonly byte CelsiusWaterFreezes = 0;
        public static readonly byte FahrenheitWaterFreezes = 32;
        public static readonly float KelvinWaterFreezes = 273.15f;
        public static readonly float RankineWaterFreezes = 491.67f;
        public static readonly byte ReaumurWaterFreezes = 0;

        /// <summary> 
        /// Function to convert Celsius to Fahrenheit. 
        /// </summary> 
        /// <param name="value">Celsius value to convert it into Fahrenheit value.</param> 
        /// <returns>Returns the Fahrenheit value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double CelsiusToFahrenheit(double value)
        {
            // 0 Celsius = 32 Fahrenheit 
            // Each ºC = 1.8 ºF 
            // Each 0.xºC = 0.18 ºF 
            // 4.5ºC = 4 x 1.8 + .5 x 0.18 + 32 = 40.1 
            return value * 9 / 5 + 32;
        }
        // CelsiusToFahrenheit 

        /// <summary> 
        /// Function to convert Celsius to Kelvin. 
        /// </summary> 
        /// <param name="value">Celsius value to convert it into Kelvin value.</param> 
        /// <returns>Returns the Kelvin value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double CelsiusToKelvin(double value)
        {
            // 0 Celsius = 273.15 Kelvin 
            return 273.15 + value;
        }
        // CelsiusToKelvin 

        /// <summary> 
        /// Function to convert Celsius to Rankine. 
        /// </summary> 
        /// <param name="value">Celsius value to convert it into Rankine value.</param> 
        /// <returns>Returns the Rankine value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double CelsiusToRankine(double value)
        {
            return value * 9 / 5 + 491.67;
        }
        // CelsiusToRankine 

        /// <summary> 
        /// Function to convert Celsius to Reaumur. 
        /// </summary> 
        /// <param name="value">Celsius value to convert it into Reaumur value.</param> 
        /// <returns>Returns the Reaumur value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double CelsiusToReaumur(double value)
        {
            // 0 Celsius = 0 Reaumur 
            return value * 4 / 5;
        }
        // CelsiusToReaumur 

        /// <summary> 
        /// Function to convert Fahrenheit to Celsius. 
        /// </summary> 
        /// <param name="value">Fahrenheit value to convert it into Celsius value.</param> 
        /// <returns>Returns the Celsius value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double FahrenheitToCelsius(double value)
        {
            return (value - 32) * (5 / 9);
        }
        // FahrenheitToCelsius 

        /// <summary> 
        /// Function to convert Fahrenheit to Kelvin. 
        /// </summary> 
        /// <param name="value">Fahrenheit value to convert it into Kelvin value.</param> 
        /// <returns>Returns the Celsius value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double FahrenheitToKelvin(double value)
        {
            return value * 5 / 9 + 255.3722;
        }
        // FahrenheitToKelvin 

        /// <summary> 
        /// Function to convert Fahrenheit to Rankine. 
        /// </summary> 
        /// <param name="value">Fahrenheit value to convert it into Rankine value.</param> 
        /// <returns>Returns the Rankine value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double FahrenheitToRankine(double value)
        {
            // 0 Fahrenheit = 459.67 Rankine 
            return value + 459.67;
        }
        // FahrenheitToRankine 

        /// <summary> 
        /// Function to convert Fahrenheit to Reaumur. 
        /// </summary> 
        /// <param name="value">Fahrenheit value to convert it into Reaumur value.</param> 
        /// <returns>Returns the Reaumur value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double FahrenheitToReaumur(double value)
        {
            // 0 Fahrenheit = 0 Reaumur 
            return (value - 32) * (4 / 9);
        }
        // FahrenheitToReaumur 

        /// <summary> 
        /// Function to convert Kelvin to Celsius. 
        /// </summary> 
        /// <param name="value">Kelvin value to convert it into Celsius value.</param> 
        /// <returns>Returns the Celsius value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double KelvinToCelsius(double value)
        {
            // 0 Kelvin = 273 Celsius 
            return value - 273;
        }
        // KelvinToCelsius 

        /// <summary> 
        /// Function to convert Kelvin to Fahrenheit. 
        /// </summary> 
        /// <param name="value">Kelvin value to convert it into Fahrenheit value.</param> 
        /// <returns>Returns the Fahrenheit value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double KelvinToFahrenheit(double value)
        {
            // ¿? 
            return 1.8 * (value - 273.15) + 32;
        }
        // KelvinToFahrenheit 

        /// <summary> 
        /// Function to convert Kelvin to Rankine. 
        /// </summary> 
        /// <param name="value">Kelvin value to convert it into Rankine value.</param> 
        /// <returns>Returns the Rankine value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double KelvinToRankine(double value)
        {
            // ¿? 
            return value * 1.8;
        }
        // KelvinToRankine 

        /// <summary> 
        /// Function to convert Kelvin to Reaumur. 
        /// </summary> 
        /// <param name="value">Kelvin value to convert it into Reaumur value.</param> 
        /// <returns>Returns the Reaumur value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double KelvinToReaumur(double value)
        {
            // ¿? 
            return 0.8 * (value - 273.15);
        }
        // KelvinToReaumur 

        /// <summary> 
        /// Function to convert Rankine to Celsius. 
        /// </summary> 
        /// <param name="value">Rankine value to convert it into Celsius value.</param> 
        /// <returns>Returns the Celsius value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double RankineToCelsius(double value)
        {
            return value * 5 / 9 - 273.15;
        }
        // RankineToCelsius 

        /// <summary> 
        /// Function to convert Rankine to Fahrenheit. 
        /// </summary> 
        /// <param name="value">Rankine value to convert it into Fahrenheit value.</param> 
        /// <returns>Returns the Fahrenheit value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double RankineToFahrenheit(double value)
        {
            // 0 Rankine = -459.67 Fahrenheit 
            return value - 459.67;
        }
        // RankineToFahrenheit 

        /// <summary> 
        /// Function to convert Rankine to Kelvin. 
        /// </summary> 
        /// <param name="value">Rankine value to convert it into Kelvin value.</param> 
        /// <returns>Returns the Kelvin value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double RankineToKelvin(double value)
        {
            // 0 Rankine = 0 Kelvin (increments of 5/9) 
            return value * 5 / 9;
        }
        // RankineToKelvin 

        /// <summary> 
        /// Function to convert Rankine to Reaumur. 
        /// </summary> 
        /// <param name="value">Rankine value to convert it into Reaumur value.</param> 
        /// <returns>Returns the Reaumur value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double RankineToReaumur(double value)
        {
            return (value - 32 - 459.67) / (9 / 4);
        }
        // RankineToReaumur 

        /// <summary> 
        /// Function to convert Reaumur to Celsius. 
        /// </summary> 
        /// <param name="value">Reaumur value to convert it into Celsius value.</param> 
        /// <returns>Returns the Celsius value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double ReaumurToCelsius(double value)
        {
            // ¿? 
            return value * 5 / 4;
        }
        // ReaumurToCelsius 

        /// <summary> 
        /// Function to convert Reaumur to Fahrenheit. 
        /// </summary> 
        /// <param name="value">Reaumur value to convert it into Fahrenheit value.</param> 
        /// <returns>Returns the Fahrenheit value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double ReaumurToFahrenheit(double value)
        {
            // ¿? 
            return value * 9 / 4 + 32;
        }
        // ReaumurToFahrenheit 

        /// <summary> 
        /// Function to convert Reaumur to Kelvin. 
        /// </summary> 
        /// <param name="value">Reaumur value to convert it into Kelvin value.</param> 
        /// <returns>Returns the Kelvin value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double ReaumurToKelvin(double value)
        {
            // ¿? 
            return value * 5 / 4 + 273.15;
        }
        // ReaumurToKelvin 

        /// <summary> 
        /// Function to convert Reaumur to Rankine. 
        /// </summary> 
        /// <param name="value">Reaumur value to convert it into Rankine value.</param> 
        /// <returns>Returns the Rankine value (temperature).</returns> 
        /// <remarks></remarks> 
        public static double ReaumurToRankine(double value)
        {
            // ¿? 
            return (value * 9 / 4) + 459.67 + 32;
        }
        // ReaumurToRankine 


        /// <summary> 
        /// Function to convert the date to Julian date. 
        /// This function can convert a Gregorian or Julian date. 
        /// </summary> 
        /// <param name="data">Date and time</param> 
        /// <returns>Returns the equivalent value of Julian date.</returns> 
        /// <remarks></remarks> 
        public static double DateToJulianDate(System.DateTime data)
        {
            // Date 
            byte dayData = Convert.ToByte(data.Day);
            byte monthData = Convert.ToByte(data.Month);
            int yearData = data.Year;
            // Time 
            byte hoursData = Convert.ToByte(data.Hour);
            byte minutesData = Convert.ToByte(data.Minute);
            byte secondsData = Convert.ToByte(data.Second);

            // Calculate the Julian value 
            double generalPortion = 100.0 * yearData + monthData - 190002.5;
            double julianValue = 367.0 * yearData;
            julianValue -= Math.Floor(7.0 * (yearData + Math.Floor((monthData + 9.0) / 12.0)) / 4.0);
            julianValue += Math.Floor(275.0 * monthData / 9.0);
            julianValue += dayData;
            julianValue += (hoursData + (minutesData + secondsData / 60.0) / 60.0) / 24.0;
            julianValue += 1721013.5;
            julianValue -= 0.5 * generalPortion / Math.Abs(generalPortion);
            julianValue += 0.5;

            // Returns the Julian Date 
            return julianValue;
        }
        // DateToJulianDate 



        /// <summary> 
        /// Function to calculate the Gregorian Christian Easter date. 
        /// </summary> 
        /// <param name="year">Year to calculate the Gregorian Christian date. The year has to be between 1583 and 2299.</param> 
        /// <returns>Returns the date calculated.</returns> 
        /// <remarks></remarks> 
        //public static System.DateTime GregorianChristianEaster(int year)
        //{
        //    // This date will be calculate between 1583 and 2299 
        //    if (year < 1583 | year > 2299)
        //    {
        //        // Return null (nothing) if the year is out of the range years 
        //        return null;
        //    }
        //    else
        //    {
        //        byte valueM = 0;
        //        byte valueN = 0;
        //        switch (year)
        //        {
        //            case (year > 1582) && (year < 1700):
        //                valueM = 22;
        //                valueN = 2;
        //                break;
        //            case year > 1699 & year < 1800:
        //                valueM = 23;
        //                valueN = 3;
        //                break;
        //            case year > 1799 & year < 1900:
        //                valueM = 23;
        //                valueN = 4;
        //                break;
        //            case year > 1899 & year < 2100:
        //                valueM = 24;
        //                valueN = 5;
        //                break;
        //            case year > 2099 & year < 2200:
        //                valueM = 24;
        //                valueN = 6;
        //                break;
        //            case year > 2199 & year < 2300:
        //                valueM = 25;
        //                valueN = 0;
        //                break;
        //        }
        //        int valueOne = year % 19;
        //        int valueTwo = year % 4;
        //        int valueThree = year % 7;
        //        int valueFour = (19 * valueOne + valueM) % 30;
        //        int valueFive = (2 * valueTwo + 4 * valueThree + 6 * valueFour + valueN) % 7;
        //        System.DateTime result = default(System.DateTime);
        //        int temporalValue = 0;
        //        if (valueFour + valueFive < 10)
        //        {
        //            temporalValue = valueFour + valueFive + 22;
        //            result = "22/03/" + year;
        //        }
        //        else
        //        {
        //            temporalValue = valueFour + valueFive - 9;
        //            if (temporalValue == 26)
        //            {
        //                temporalValue = 19;
        //            }
        //            else if (temporalValue == 25 & valueFour == 28 & valueFive == 6 & valueOne > 10)
        //            {
        //                temporalValue = 18;
        //            }
        //            result = temporalValue + "/04/" + year;
        //        }
        //        // Returns the value 
        //        return result;
        //    }
        //}
        // GregorianChristianEaster 


    } // Conversion

} // SMS.Framework.Core